package com.fsi.client.util;

import com.fsi.client.ApplicationServiceDelegate;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PhotoPopup implements ClickListener, KeyPressHandler {
	private final static ApplicationServiceDelegate service = new ApplicationServiceDelegate();
	protected static final int SHOWPHOTOS_METHOD = 3;
	private static final int SIZEDIFF = 80;
	static PopupPanel glass;
	static PopupPanel popup;
	static String s;
	Image photo = new Image();
	Button closeButton;
	PushButton setMainButton;
	Button deleteButton;
	HorizontalPanel buttonPanel;
	HorizontalPanel photoPanel;
	int screenWidth;
	int screenHeight;
	int newHeight, newWidth;
	int x, y;

	public PhotoPopup(final String customerID, final String photoID,
			String title, int origHeight, int origWidth) {
		glass = new PopupPanel();
		glass.setStyleName("rx-glass");
		photoPanel = new HorizontalPanel();
		closeButton = new Button("Close");
		deleteButton = new Button("Delete");
		setMainButton = new PushButton(new Image("images/check.jpg"),
				new Image("images/cross.jpg"));
		buttonPanel = new HorizontalPanel();

		DOM.setAttribute(glass.getElement(), "width", "100%");
		DOM.setAttribute(glass.getElement(), "height", "100%");

		popup = new PopupPanel(false);
		popup.setStyleName("rx-popup");

		VerticalPanel popupContents = new VerticalPanel();
		Label header = new Label(title);
		header.setStyleName("rx-popup-header");

		photoPanel.setStyleName("bp-photopanel");
		photoPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		photoPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		photo.setUrl(GWT.getModuleBaseURL() + "getphoto?nick=" + photoID);

		int scrHeight = Window.getClientHeight();
		int scrWidth = Window.getClientWidth();
		if (origHeight > scrHeight - SIZEDIFF) {
			newHeight = scrHeight - SIZEDIFF;
			s = Integer.toString(newHeight) + "px";
			photo.setHeight(s);
			System.out.println(s);
		} else
			newHeight = origHeight;
		newWidth = origWidth;

		y = (scrHeight - newHeight) / 2;
		x = 10;

		System.out.println(scrHeight);
		System.out.println(photo.getHeight());
		System.out.println(y);

		photo.addLoadHandler(new LoadHandler() {

			@Override
			public void onLoad(LoadEvent event) {
			}
		});

		closeButton.setStyleName("menuButton");
		deleteButton.setStyleName("menuButton");
		closeButton.addClickListener(this);

		photoPanel.add(photo);

		buttonPanel.add(closeButton);
		buttonPanel.add(new Label("Is Main"));
		buttonPanel.add(setMainButton);
		buttonPanel.add(deleteButton);

		popup.add(popupContents);
		popupContents.add(header);
		popupContents.add(photoPanel);
		popupContents.add(buttonPanel);

		photoPanel.setStylePrimaryName("photoPanel");

		glass.show();
		popup.setPopupPosition(10, 10);
		popup.show();
		// popup.center();

		deleteButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				service.deletePhoto(customerID, photoID);
				glass.hide();
				popup.hide();
			}
		});

		setMainButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				PushButton pb = (PushButton) event.getSource();
			}
		});

	}

	public void close() {
		glass.hide();
		popup.hide();
	}

	@Override
	public void onClick(Widget sender) {
		close();
	}

	@Override
	public void onKeyPress(KeyPressEvent event) {
		if (KeyCodes.KEY_ESCAPE == event.getCharCode()) {
			close();
		}
	}
}
